package keter.security;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import keter.framework.util.SecurityUtil;
import keter.model.User;

/**
 * <p>
 * Class : keter.web.security.KeterUserDetailsService
 * <p>
 * Descdription: 返回用户详细信息
 * 
 * @author gulixing@msn.com
 * @version 1.0.0
 */
@Service("UserDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LoggerFactory.getLogger(UserDetailsServiceImpl.class);
	
	
	/**
	 * <p>从数据库中读取用户，由SS判断用户密码是否正确
	 * 
	 * @see org.springframework.security.core.userdetails.UserDetailsService#loadUserByUsername(java.lang.String)
	 * @author  gulixing@msn.com
	 * @date    2015年12月22日
	 */
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		    User user = User.dao.findByUsername(username);
		    if(user == null){//用户不存在
			    logger.error("用户[{}]不存在",username);
		    	SecurityUtil.log("用户"+username+"不存在", SecurityUtil.EVENT_TYPE.AUTHENTICATION);
		    	throw new UsernameNotFoundException("用户["+username+"]不存在");
		    }
			UserAdapter ua = new UserAdapter(user);							
//		    if (ua.getAccount().getStatus().equals(Account.Status.DISABLED.toString())){
//		    	throw new DisabledException("您的账户已被禁用，请联系管理员！");
//		  	}
//		    if (ua.getAccount().getStatus().equals(Account.Status.LOCKED.toString())){
//		    	throw new LockedException("您的账户已被锁定，请联系管理员！");
//		    }
			return ua;
	}
}
